package com.nexfra.bbssys.common.controller;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.nexfra.bbssys.common.exception.GenericException;

@ControllerAdvice
public class DefaultExceptionController {

	private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionController.class);

    /**
     * GenericException 유형의 예외 발생시 처리하는 메소드
     *
     * @param class GenericException class
     * @return Exception object
     */
	@ExceptionHandler(GenericException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public ModelAndView handleGenericException(GenericException ex) {
		logger.error("", ex);
		
		ModelAndView mav = new ModelAndView("/common/exception");
		mav.addObject("exception", ex);
		mav.addObject("message", ex.getMessage());
		return mav;
	}
	
    /**
     * Exception 유형의 예외 발생시 처리하는 메소드.
     * 이 메소드는 Exception객체를 GenericException객체로 wrapping하여 리턴한다.
     *
     * @param class GenericException class
     * @return Exception object
     */
	@ExceptionHandler(Exception.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public ModelAndView handleException(Exception ex) {
		logger.error("", ex);
		
		ModelAndView mav = new ModelAndView("/common/exception");
		mav.addObject("exception", ex);
		mav.addObject("message", ex.getMessage());
		return mav;
	}	
}